﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace BTServer
{
    public partial class Login : Form
    {
        public Login()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string username = txtUsername.Text;
            string password = txtPassword.Text;
            int isLoginValid = 0;
            medicalstaff logedStaff = null;
            MySqlConnection conn = ConnectionPool.checkOutConnection();
            heartdb context = new heartdb(conn);
            var upit = from ms in context.medicalstaff where (ms.Username == username) & (ms.Password == password) select ms;
            foreach (var u in upit)
            {
                isLoginValid++;
                logedStaff = (medicalstaff)u;
            }
            if (isLoginValid > 0)
            {
                Main m = new Main(logedStaff);
                this.Hide();
                ConnectionPool.checkInConnection(conn);
                if (m.ShowDialog() == DialogResult.Cancel)
                {
                    this.Show();
                }
            }
            else
            {
                MessageBox.Show("Korisničko ime ili lozinka su pogrešni", "Greška prilikom logovanja", MessageBoxButtons.OK, MessageBoxIcon.Stop); 
            }

        }
    }
}
